<template>
  <div class="inventory-change-record">
    <div class="card inventory-change-record__header">
      <el-form inline>
        <el-form-item label="物品类型">
          <el-cascader
            v-model="params.goodsTypeId"
            :options="goodsTypeOptions"
            :props="{ value: 'id', label: 'name' }"
            clearable
          />
        </el-form-item>
        <el-form-item label="记录来源">
          <el-select
            v-model="params.type"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="记录时间">
          <el-date-picker
            v-model="params.time"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            size="medium"
            type="danger"
            @click="handleReset"
          >重置</el-button>

          <el-button
            size="medium"
            type="primary"
            @click="handleSearch"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="card">
      <div style="margin-bottom: 20px;">
        <el-alert
          show-icon
          title="注意：该处的变更数量单位都为 ”主计量单位” "
          type="warning"
        />
      </div>

      <el-table
        v-loading="loading"
        :data="list"
        border
        stripe
      >
        <el-table-column
          label="物品类型"
          prop="goodsType.name"
          align="center"
        />

        <el-table-column
          label="记录来源"
          prop="recordType.desc"
          align="center"
        />

        <el-table-column
          label="变更数量"
          prop="quantity"
          align="center"
        >
          <template v-slot="{ row }">
            {{ `${row.quantity}${row.goodsType?.goods_type_main_unit?.name}` }}
          </template>
        </el-table-column>

        <el-table-column
          label="记录时间"
          prop="created_at"
          align="center"
        >
          <template v-slot="{ row }">
            {{ timeFormat(row.created_at) }}
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <Pagination
          style="margin-top: 0; padding: 0;"
          :total="total"
          :limit="params.paginate"
          @pagination="pagination"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { getGoodsType, getInventoryChangeRecord, getInventoryRecordType } from '@/api/officeSuppliesManagement'
import dayjs from 'dayjs'
import Pagination from '@/components/Pagination/index.vue'
import { sleep } from '@/utils'
export default {
  name: '',
  components: { Pagination },
  data() {
    return {
      list: [],
      params: {
        goodsTypeId: null,
        type: null,
        page: 1,
        paginate: 10,
        time: []
      },
      total: 0,
      typeOptions: [],
      goodsTypeOptions: [],
      loading: false,
      pickerOptions: {
        disabledDate(time) {
          const now = Date.now()
          return time.getTime() > now
        }
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      Promise.allSettled([
        this.getGoodsType(),
        this.getInventoryChangeRecord(),
        this.getInventoryRecordType()
      ])
    },

    async getGoodsType() {
      try {
        const res = await getGoodsType()
        this.goodsTypeOptions = res.data
      } catch (error) {
        throw new Error(error?.response?.data?.message || '信息获取失败')
      }
    },

    async getInventoryChangeRecord() {
      this.loading = true
      try {
        const res = await getInventoryChangeRecord(this.generateParams())
        this.list = res.data
        this.total = res.meta.total
      } catch (error) {
        this.$message.error(error?.response?.data?.message || '信息获取失败')
      } finally {
        await sleep()
        this.loading = false
      }
    },

    async getInventoryRecordType() {
      try {
        const res = await getInventoryRecordType()
        this.typeOptions = res.data.map(item => {
          return {
            label: item.desc,
            value: item.id
          }
        })
      } catch (error) {
        throw new Error(error?.message || '信息获取失败')
      }
    },

    generateParams() {
      const len = this.params.goodsTypeId?.length
      const goodsTypeId = len > 0 ? this.params.goodsTypeId[len - 1] : null
      return {
        ...this.params,
        time: this.params.time && this.params.time.length ? JSON.stringify(this.params.time) : null,
        goodsTypeId
      }
    },

    timeFormat(t) {
      return dayjs(t).format('YYYY-MM-DD HH:mm:ss')
    },

    pagination(page) {
      this.params.page = page.page
      this.params.paginate = page.limit
      this.getInventoryChangeRecord()
    },

    handleSearch() {
      this.params.page = 1
      this.getInventoryChangeRecord()
    },

    handleReset() {
      this.params = this.$options.data().params
      this.getInventoryChangeRecord()
    }
  }
}
</script>
<style lang="scss" scoped>
.inventory-change-record {
  padding: 20px;

  &__header {
    padding: 20px 20px 0 20px !important;
    ::v-deep(.el-form-item) {
      margin-bottom: 20px;
      margin-right: 20px;
      //padding: 0 20px;
    }
  }

  .card {
    padding: 20px;
    background-color: #fff;
    border-radius: 4px;
    margin-bottom: 20px;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: end;
  }
}
</style>
